const md = require('../../utils/md5.js');
const app = getApp();
const post = require('../../utils/post.js');
const util = require('../../utils/util.js');
Page({

  data: {
    img1: "http://eplus.zokoo.com.cn/upload/wallet/nplus.png",
    img2: "http://eplus.zokoo.com.cn/upload/wallet/nplus.png",
    servicename: wx.getStorageSync('userinfo').MebName,
  },

  onLoad: function (options) {
    var that = this;
    console.log(options);
    let etype = options.etype ? options.etype:'';
    let accid = options.accid ? options.accid : '';
    var time = util.formatTime(new Date());
    let timedetail = time.split(' ')[1];
    time = time.split(' ')[0];
    
    time = time.split('-');
    that.setData({
      year: time[0],
      month: time[1],
      day: time[2],
      etype: etype,
      accid: accid,
      timedetail: timedetail
    })
    
  },
  beagree: function () {
    wx.setStorageSync('auth', 1);
    wx.navigateBack({
      delta: 1
    })
  },
  chooseimg1(e) {
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        console.log(tempFilePaths);
        var apinameIsMeb = "/File/Upload";
        var timestamp = new Date().getTime();
        timestamp = parseInt(timestamp / 1000);
        var md5IsMeb = md.hexMD5(app.server.appid + timestamp + apinameIsMeb + app.server.appkey);
        wx.showLoading({
          title: '上传中...',
        })
        wx.uploadFile({
          url: app.server.url + apinameIsMeb, //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: 'file[]',
          method: 'POST',
          header: { 'content-type': 'multipart/form-data' },
          formData: {
            "sign": md5IsMeb,
            "appid": app.server.appid,
            "tmsp": timestamp,
            "objID": 666,
            "file": that.data.year + "/" + that.data.month + "/" + that.data.day + "/" + new Date().getTime() + Math.ceil(Math.random() * 1000) + ".jpg"
          },
          success: function (res) {
            wx.hideLoading();
            var data = res.data;
            data = JSON.parse(data);
            console.log(data.data)
            if (data.data != '') {
              that.setData({
                img1: "http://eplus.zokoo.com.cn/upload/" + data.data,
                idcardimg1: "http://eplus.zokoo.com.cn/upload/" + data.data
              })
            }
          }
        })
      }
    })
  },
  chooseimg2(e) {
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        console.log(tempFilePaths);
        var apinameIsMeb = "/File/Upload";
        var timestamp = new Date().getTime();
        timestamp = parseInt(timestamp / 1000);
        var md5IsMeb = md.hexMD5(app.server.appid + timestamp + apinameIsMeb + app.server.appkey);
        wx.showLoading({
          title: '上传中...',
        })
        wx.uploadFile({
          url: app.server.url + apinameIsMeb, //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: 'file[]',
          method: 'POST',
          header: { 'content-type': 'multipart/form-data' },
          formData: {
            "sign": md5IsMeb,
            "appid": app.server.appid,
            "tmsp": timestamp,
            "objID": 666,
            "file": that.data.year + "/" + that.data.month + "/" + that.data.day + "/" + new Date().getTime() + Math.ceil(Math.random() * 1000) + ".jpg"
          },
          success: function (res) {
            wx.hideLoading();
            var data = res.data;
            data = JSON.parse(data);
            console.log(data.data)
            if (data.data != '') {
              that.setData({
                img2: "http://eplus.zokoo.com.cn/upload/" + data.data,
                idcardimg2: "http://eplus.zokoo.com.cn/upload/" + data.data
              })
            }
          }
        })
      }
    })
  },
  formSubmit: function (e) {
    var that = this;
    var dataform = e.detail.value;
    console.log('form发生了submit事件，携带数据为：', e.detail.value);
    if (!dataform.acctivetype){
      wx.showModal({
        title: '',
        content: '没有事件类型，请退出重试！',
      })
      return;
    }
    if (!dataform.recordDesc) {
      wx.showModal({
        title: '',
        content: '请填写事件描述！',
      })
      return;
    }
    wx.showLoading({
      title: '提交中...',
    })
    var data = {};
    let datas = {};
    datas.ServiceMebID = wx.getStorageSync('userinfo').MebID;
    datas.ActiveDate = that.data.year + "-" + that.data.month + "-" + that.data.day + " " + that.data.timedetail;
    datas.ActiveTitle = dataform.recordName;
    datas.ActiveType = dataform.acctivetype;
    datas.ActiveDetail = dataform.recordDesc;
    datas.PhotoUrl1 = dataform.img1;
    datas.PhotoUrl2 = dataform.img2;
    datas.DepID = that.data.accid;
    data.data = JSON.stringify(datas);
    console.log(data);
    var url = "/BO/PE_ActiveHistory/Add";
    var callback = function (res) {
      console.log(res);
      wx.hideLoading();
      if(res.data.code==0){
        wx.navigateBack({
          delta:1
        })
      }
    };
    var res = post.getdata(url, data, callback);
  },
})